import { defineStore } from "pinia";
import { userKey } from "./keys";
interface IUserState{
  menus:IMenuModel[];
  bool:boolean;
  flag:boolean;
  w:number;
  username:string;
}
type menu = IMenuModel & { children: IMenuModel[] }
export const useUserStore = defineStore(userKey, {
  state():IUserState {
    return {
      bool: false,
      flag:false,
      w:250,
      menus:[],
      username:'',
    };
  },
  getters:{
    routes(){
      let menus: menu[] = [];
      menus = this.menus.filter((item) => item.parentId === 0).map(item => ({
        ...item,
        children: [],
      }));
      this.menus.forEach(sub=>{
        const p = menus.find((item)=>item.id===sub.parentId)
        // if(p && sub.hidden===0){
        //   p.children.push(sub)
        // } 
         if(p){
          p.children.push(sub)
        }
      })
      return menus;
    },
    permissions(): string[] {
      return this.menus.map((item) => item.name);
    },
  },
actions:{
  isShow(){
    // this.bool=!this.bool
    if(this.flag=!this.flag){
      this.bool=true
      this.w=65
  }else{
       this.bool=false
       this.w=250
  }
  }
}
});
